from tkinter import *
# from ..server import settings  # 此为本地调试模式
from tkinter import messagebox
import re
# from .. import login
class Ragedit ():

    def __init__(self):
        self.seconds = None
        self.malibox_None = False
        self.root = Tk()
        self.root.title("注册系统")
        self.root.config(background="white")
        self.root.resizable(0, 0)
        root_height = 350
        root_width = 460
        sx = self.root.winfo_screenwidth()
        sy = self.root.winfo_screenheight()
        cw_x = (sx - root_width) / 2
        cw_y = (sy - root_height) / 2
        self.root.geometry("%dx%d+%d+%d" % (root_width, root_height, cw_x, cw_y))
        self.canvas_top = Canvas(
            self.root,
            bg="white",
            height=2,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        self.canvas_top.pack(side=TOP, fill=Y)
        Label(self.canvas_top,text = "用户注册", font=("宋", 15, "bold"), fg="#32CDFA",bg = "white").grid(row=0, column=0,
                                                                                           pady = 15)
        Canvas(self.canvas_top,bg ="#32CDFA" ,width = 320,height = 2,relief="ridge" ,bd = 0,highlightthickness=0,).grid(row=1, column=0,
                                                                                           )

        self.canvas_top_1 = Canvas(
            self.root,
            bg="white",
            height=2,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        self.canvas_top_1.pack(side=TOP, fill=Y,pady = 20)
        # 注册用户名
        self.new_name = StringVar()
        Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA", text = "用户名:",width = 8).grid(row = 0,column = 0,pady = 6,padx = 15)
        Entry(self.canvas_top_1,textvariable = self.new_name, bd=3).grid(row=0, column=1, pady=6, padx=15)
        # 注册密码
        self.new_password = StringVar()
        Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "密    码:",width = 8).grid(row = 1,column = 0,pady = 6,padx = 15)
        Entry(self.canvas_top_1,textvariable = self.new_password, bd=3,show="*" ).grid(row=1, column=1, pady=6, padx=15)
        # 注册确认密码
        self.new_password_confirm = StringVar()
        Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "确认密码:",width = 8).grid(row = 2,column = 0,pady = 6,padx = 15)
        Entry(self.canvas_top_1,textvariable = self.new_password_confirm, bd=3,show="*" ).grid(row=2, column=1, pady=6, padx=15)
        # 注册邮箱
        self.new_mailbox = StringVar()
        Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "邮    箱:",width = 8).grid(row = 3,column = 0,pady = 6,padx = 15)
        Entry(self.canvas_top_1,textvariable = self.new_mailbox, bd=3, ).grid(row=3, column=1, pady=6, padx=15)

        # 邮箱验证码和发送
        self.new_mailbox_confirm = StringVar()
        Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "验证码:",width = 8).grid(row = 4,column = 0,pady = 6,padx = 15)
        frame1 = Frame(self.canvas_top_1, bg="white", relief="flat", height=20, width=20)
        frame1.grid(row=4, column=1)
        Entry(frame1,textvariable = self.new_mailbox_confirm, bd=3, width=15).pack(side=LEFT, fill=Y)
        self.end_b = Button(frame1, bg="#32CDFA", fg="white", text="发送", relief="flat", bd=0,command =lambda : self.verification(60))
        self.end_b.pack(side=LEFT, fill=Y, padx=3)







        # 提示框
        self.new_name_ts = Label(self.canvas_top_1, bg="white", fg="red", width=8)
        self.new_name_ts.grid(row=0, column=2, pady=6)
        self.new_password_ts = Label(self.canvas_top_1, bg="white", fg="red", width=8)
        self.new_password_ts.grid(row=1, column=2, pady=6)
        self.new_password_confirm_ts = Label(self.canvas_top_1, bg="white", fg="red", width=8)
        self.new_password_confirm_ts.grid(row=2, column=2, pady=6)
        self.new_mailbox_ts = Label(self.canvas_top_1, bg="white", fg="red", width=8)
        self.new_mailbox_ts.grid(row=3, column=2, pady=6)
        self.new_mailbox_confirm_ts = Label(self.canvas_top_1, bg="white", fg="red", width=8)
        self.new_mailbox_confirm_ts.grid(row=4, column=2, pady=6)



        Button(self.canvas_top_1,bg = "#32CDFA",fg = "white",text = "提交注册",relief="flat",bd = 0,width = 20,command = self.register_confirm).grid(row = 5,column = 0,columnspan=3,pady = 5)




        self.root.mainloop()



    def register_confirm(self):
        # 提示框清除
        self.new_name_ts.config(text="")
        self.new_password_ts.config(text="")
        self.new_password_confirm_ts.config(text = "")
        self.new_mailbox_ts.config(text = "")
        self.new_mailbox_confirm_ts.config(text = "")

        name = self.new_name.get()
        password = self.new_password.get()
        password_confirm = self.new_password_confirm.get()
        x_mailbox = self.new_mailbox.get()
        mailbox_confirm = self.new_mailbox_confirm.get()

        try:
            # 查找数据库用户名是否存在
            readname_sql = f'''select * from b_user where username = "{name}"'''
            data = settings.Mysql_c(readname_sql)
            user_data = data.cursor_1()

            readmalibox_sql = f'''select * from b_user where mailbox = "{x_mailbox}"'''
            mailbox_data = settings.Mysql_c(readmalibox_sql)
            x_mailbox_data = mailbox_data.cursor_1()

            # 密码字母数字8-16位判断
            r = '^(?=.*?\d)(?=.*[a-z])[0-9a-z]{8,16}$'
            result_paw = re.match(r, password)

            r_malibox = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
            result_malibox = re.match(r_malibox,x_mailbox)
            pwdmd5 = settings.Md5(pwd=password)
            ser_password = pwdmd5.strmd5()

            if not name:
                self.new_name_ts.config(text = "!注册账号不能空",width = 13)
            else:
                if user_data.real:
                    self.new_name_ts.config(text="!账号已存在,\n请输入其他账号", width=13)

                elif not password:
                    self.new_password_ts.config(text = "!注册密码\n不能为空",width  = 13)
                elif result_paw is None:
                    print()
                    self.new_password_ts.config(text="!请输入8-16位密\n码,包含数字和字母", width=13)
                elif password != password_confirm:
                    self.new_password_confirm_ts.config(text = "!两次输入的\n密码不一样" , width = 13)
                elif not x_mailbox:
                    self.new_mailbox_ts.config(text = "!请输入邮箱", width = 13)
                elif result_malibox is None:
                    self.new_mailbox_ts.config(text = "!邮箱格式错误", width = 13)
                elif x_mailbox_data.real:
                    self.new_mailbox_ts.config(text = "!此邮箱已绑\n定其他账户", width = 13)
                elif not mailbox_confirm:
                    self.new_mailbox_confirm_ts.config(text="!验证码\n不能为空")
                elif mailbox_confirm != self.ver_random:
                    self.new_mailbox_confirm_ts.config(text="!验证码错误",width = 13)
                elif self.malibox_None:
                    self.new_mailbox_confirm_ts.config(text = "!验证码已过期", width = 13)

                else:
                    sql = f"""insert into b_user(username,passwd,mailbox) values("{name}","{ser_password}","{x_mailbox}")"""
                    sql_data = settings.Mysql_c(sql = sql)
                    sql_data.commit()
                    messagebox.showinfo(title="恭喜", message="登录成功")
                    self.destro()



        except IOError:
            pass



    def verification(self,ti):

        self.new_mailbox_ts.config(text="")

        x_mailbox = self.new_mailbox.get()

        r_malibox = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
        result_malibox = re.match(r_malibox, x_mailbox)

        readmalibox_sql = f'''select * from b_user where mailbox = "{x_mailbox}"'''
        mailbox_data = settings.Mysql_c(readmalibox_sql)
        x_mailbox_data = mailbox_data.cursor_1()
        if not x_mailbox:
            self.new_mailbox_ts.config(text="!请输入邮箱", width=13)
        elif result_malibox is None:
            self.new_mailbox_ts.config(text="!邮箱格式错误", width=13)
        elif x_mailbox_data.real:
            self.new_mailbox_ts.config(text="!此邮箱已绑\n定其他账户", width=13)
        else:
            self.ver_random = settings.Random().code()
            self.ver_smtplib = settings.Smtplib(theme="注册", malibox= x_mailbox, value = self.ver_random)
            self.seconds = ti
            self.refresh_current_time()
    def refresh_current_time(self):
        self.malibox_None = False
        self.end_b.config(state=DISABLED)

        """刷新当前时间"""
        if self.seconds > 0:


            self.end_b.config(text=str(self.seconds),width = 3)
            self.seconds -= 1
            self.end_b.after(1000, self.refresh_current_time)
        else:
            self.end_b.config(state=NORMAL)
            self.end_b.config(text="发送",width = 3)
            self.malibox_None = True

    def destro(self):
        self.root.destroy()
        login.gui.Login()



if __name__ == "__main__":
    Ragedit()
